<template>
  <FormView
    v-model="entity"
    :title="entity.id ? '修改通知公告' : '新建通知公告'"
    :toolbar-attrs="{ buttons: ['back', { save: entity.status === '0' }, 'cancel'] }"
    :toolbar-listeners="{ save: persist }">
    <template #toolbar-right>
      <ConfirmButton
        v-if="entity.id && entity.status === '0'"
        type="primary"
        message="确认发布公告吗?"
        confirm-title="提示"
        confirm-type="warning"
        icon="el-icon-s-promotion"
        size="mini"
        @click="pushBulletin()">
        发布
      </ConfirmButton>
    </template>
    <FormHolder ref="form" label-width="110px" :disabled="isDisabled" :model="entity">
      <FormItemGroup block-title="基本信息">
        <FormSelect
          v-model="entity.typeCode"
          prop="typeCode"
          required
          label="类 别"
          emit-label
          :option-label.sync="entity.typeName"
          dict-type="bulletin_type" />
        <FormSelect v-model="entity.status" prop="status" required label="状态" dict-type="bulletin_status" disabled />
        <FormInput v-model="entity.subject" prop="subject" label="主题" :span="24" required />
        <FormRichTextEditor v-model="entity.content" prop="content" label="内容" block required />
        <FormTextarea v-model="entity.remarks" rows="3" prop="remarks" label="备注" max-length="2000" />
      </FormItemGroup>
      <FormItemGroup :gutter="0" block-title="附件信息">
        <UploadPage
          v-if="entity.unid"
          ref="UploadPage"
          style="padding: 0"
          :unique-code="entity.unid"
          model="purchaseOrder"
          classify="purchaseOrder" />
      </FormItemGroup>
    </FormHolder>
  </FormView>
</template>

<script>
  import { addObj, putObj, getObj, create, pushBulletin } from '@/api/admin/bulletin'
  import { addBrowseRecord } from '@/api/admin/bulletinreadrecord'
  import form from '@/vue/mixin/form'

  export default {
    name: 'BulletinForm',
    relativeViewId: 'BulletinView',
    mixins: [form],
    data() {
      return {
        entity: {
          createBy: '',
          createId: 0,
          createTime: '',
          deptFullname: '',
          deptId: 0,
          executionFullname: '',
          executionId: null,
          expenditureFullname: '',
          expenditureId: null,
          id: '',
          remarks: '',
          type: '',
          unid: '',
          updateBy: '',
          subject: '',
          content: '',
          typeCode: '',
          typeName: '',
          status: '',
          updateTime: ''
        }
      }
    },
    created() {
      this.init()
    },
    methods: {
      create,
      load: getObj,
      save: addObj,
      update: putObj,
      async init() {
        if (this.entity.id) {
          await addBrowseRecord(this.entity.id)
        }
      },
      afterInitForm() {
        this.disabled = this.entity.status !== '0'
      },
      async pushBulletin() {
        const res = await pushBulletin([this.entity.id])
        if (res.data.data === 0) {
          this.$message.success('发布成功')
          await this.initForm()
        } else {
          this.$message.warning('发布失败')
        }
      }
    }
  }
</script>
